package com.party.controller.sys.exam;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.Exam;
import com.party.model.SysUser;
import com.party.service.exam.ExamService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/exam")
public class SysExamController {

    @Resource
    ExamService examService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/exam/index");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String name = req.getParameter("name");
        String testTime = req.getParameter("testTime");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        Exam exam = new Exam();
        exam.setName(name);
        if(testTime != null && !"".equals(testTime))
        {
            if(Tools.isNumber(testTime)){
                exam.setTesttime(Integer.parseInt(testTime));
            }
        }
        Page p = this.examService.getExamTestList(exam, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/exam/add");
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
                                      @ModelAttribute("exam") Exam exam,
                                      HttpSession session,
                                      HttpServletRequest request,
                                      HttpServletResponse response) {

        String msg="";
        if ((SysUser) session.getAttribute("sysUser") != null) {
            int uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            exam.setCreatid(uid);
        }

        String time=Tools.getNowDate();
        exam.setCreattime(time);
        int num=examService.addExam(exam);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        Exam exam=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            exam=examService.getExamByPrimaryKey(intId);
        }

        if(exam==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }

        modelAndView.addObject("exam",exam);
        modelAndView.setViewName("/sys/exam/edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
                                  @ModelAttribute("exam") Exam exam,
                                  HttpSession session,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        String msg="";
        int num=examService.updateExamByPrimaryKey(exam);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = examService.deleteExamByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
